home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Programming / Python-1.4 / Source / Amiga / testset / as.py next >
Text File  |  1997-01-12  |  2KB  |  60 lines

  1. import site
  2. import ARexx
  3. import socket
  4.  
  5. # dictionary of used sockets, filedes->socket
  6. sockets={}
  7.  
  8. # One function to handle the socket commands,
  9. # this is convenient because one 'try' block covers all.
  10. def cmd_dispatcher(host,msg,cmd,args):
  11.     try:
  12.         if cmd=='GETHOSTBYNAME':
  13.             msg.result=socket.gethostbyname(args['HOST'])
  14.         elif cmd=='GETHOSTNAME':
  15.             msg.result=socket.gethostname()
  16.         elif cmd=='GETHOSTBYADDR':
  17.             (msg.result,aliaslist,ipaddrlist) = socket.gethostbyaddr(args['IPADDR'])
  18.         elif cmd=='GETSERVBYNAME':
  19.             if args['TCP']: prot='tcp'
  20.             if args['UDP']: prot='udp'
  21.             msg.result=`socket.getservbyname(args['SERVICE'],prot)`
  22.         elif cmd=='CONNECTSOCKET':
  23.             sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  24.             sock.connect(args['HOST'],args['PORT'])
  25.             sockets[sock.fileno()]=sock
  26.             msg.result=`sock.fileno()`
  27.         elif cmd=='CLOSESOCKET':
  28.             fd=args['SOCKET']
  29.             sockets[fd].close()
  30.             del sockets[fd]
  31.         elif cmd=='READSOCKET':
  32.             msg.result=sockets[args['SOCKET']].recv(args['LEN'])
  33.             if not msg.result: msg.rc=ARexx.RC_WARN
  34.         elif cmd=='WRITESOCKET':
  35.             msg.result=`sockets[args['SOCKET']].send(args['DATA'])`
  36.         elif cmd=='WRITESOCKETCR':
  37.             msg.result=`sockets[args['SOCKET']].send(args['DATA']+'\n')`
  38.  
  39.     except socket.error,string:
  40.         msg.rc=ARexx.RC_ERROR
  41.         if type(string)==type(''): msg.rc2=string
  42.         else: msg.rc2=string[1]
  43.     return 1
  44.  
  45. cmds = [
  46. ('GETHOSTBYNAME','HOST/A',None,cmd_dispatcher),
  47. ('GETHOSTNAME','',None,cmd_dispatcher),
  48. ('GETHOSTBYADDR','IPADDR/A',None,cmd_dispatcher),
  49. ('GETSERVBYNAME','SERVICE/A,TCP/S,UDP/S',{'TCP':-1,'UDP':0},cmd_dispatcher),
  50. ('CONNECTSOCKET','HOST/A,PORT/N/A',None,cmd_dispatcher),
  51. ('CLOSESOCKET','SOCKET/A/N',None,cmd_dispatcher),
  52. ('READSOCKET','SOCKET/A/N,LEN/A/N',None,cmd_dispatcher),
  53. ('WRITESOCKET','SOCKET/A/N,DATA/A/F',None,cmd_dispatcher),
  54. ('WRITESOCKETCR','SOCKET/A/N,DATA/A/F',None,cmd_dispatcher)
  55. ]
  56.  
  57. h=ARexx.host('PY_SOCK',cmds)
  58.  
  59. h.run()
  60.